/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hps.model;

import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import javax.swing.ImageIcon;

/**
 *
 * @author $Author: schmidts.oli $
 * @version $Revision: 69 $
 * @date $Date: 2013-01-09 19:48:55 +0000 (Wed, 09 Jan 2013) $
 */
public class Explosion implements IInfluence
{

    public static final int STANDARD_SPREAD_TIME = 10;
    public static final int STANDARD_RANGE = 4;
    //static
    private static Image explosionImage = null;
    //Member-Variablen
    private int ticksToSpread;
    private Point position;
    private int range;
    private ArrayList<IInfluence> me;

    /**
     *
     * @param ticks
     * @param p
     * @param range
     */
    public Explosion(int ticks, Point p, int range)
    {
        this.ticksToSpread = ticks;
        this.position = p;

        this.me = new ArrayList<IInfluence>();
        this.me.add(this);

    }

    @Override
    public void setInfluence(FloorField field)
    {
    }

    @Override
    public ArrayList<IInfluence> spread(IInfluence[][] blocked)
    {
        if (ticksToSpread > -1)
        {
            ticksToSpread--;
        }
        return me;
    }

    @Override
    public int getBlocked(Point p)
    {
        return 0;
    }

    @Override
    public ArrayList<Point> getBlockedFields()
    {
        return new ArrayList<Point>();
    }

    @Override
    public boolean isBlocking()
    {
        return false;
    }

    @Override
    public IInfluence clone()
    {
        return new Explosion(ticksToSpread, position, range);
    }

    /**
     * Fuegt den Pedestrians in Reichweite Schaden zu.
     * Je naeher die Pedestrians sind, desto mehr Schaden erhalten sie.
     * 
     * @param pedestrians Pedestrians im Raum
     */
    @Override
    public void damage(ArrayList<Pedestrian> pedestrians) {
        for(Pedestrian p: pedestrians)
        {
            if (this.position.distance(p.getPos()) < this.range)
            {
                p.inflictDamage((int)((1-this.position.distance(p.getPos())/range)*100-20));
            }
        }
    }

    @Override
    public void draw(Graphics g, Point roomPosition, int singleFieldSize)
    {
        if (explosionImage == null)
        {
            explosionImage = new ImageIcon(getClass().getResource("/hps/view/HPS_icons/explosion.png")).getImage();
        }
        g.drawImage(explosionImage,
                roomPosition.getX() + this.position.getX() * singleFieldSize + 1,
                roomPosition.getY() + this.position.getY() * singleFieldSize + 1,
                singleFieldSize, singleFieldSize, null);
    }

    @Override
    public boolean isAt(Point p)
    {
        return position.equals(p);
    }

    public void explode(ArrayList<Pedestrian> peds)
    {
        for (int i = 0; i <= range; i++)
        {
            int d = (5 - range * 4) / 4;
            int x = 0;
            int y = range;
            Point[] p = new Point[8];

            //Bresenham's circle algorithm
            do
            {
                p[0] = new Point(position.getX() + x, position.getY() + y);
                p[1] = new Point(position.getX() + x, position.getY() - y);
                p[2] = new Point(position.getX() - x, position.getY() + y);
                p[3] = new Point(position.getX() - x, position.getY() - y);
                p[4] = new Point(position.getX() + y, position.getY() + x);
                p[5] = new Point(position.getX() + y, position.getY() - x);
                p[6] = new Point(position.getX() - y, position.getY() + x);
                p[7] = new Point(position.getX() - y, position.getY() - y);
                for (Pedestrian ped : peds)
                {
                    for (Point p1 : p)
                    {
                        if (ped.isAt(p1))
                        {
                            ped.setHealth(ped.getHealth() - 50);
                        }
                    }
                }
                if (d < 0)
                {
                    d += 2 * x + 1;
                }
                else
                {
                    d += 2 * (x - y) + 1;
                    y--;
                }
            } while (x <= y);
        }
    }
}
